package com.myidis.servlet;

import java.util.ArrayList;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auxiliary.NeuralNetwork;
import com.auxiliary.QuotaDataFinder;
import com.auxiliary.SeasonAdjuster;
import com.auxiliary.TimeSequence;
import com.myidis.request.BBReq;
import com.myidis.request.SeasonAdjustReq;
import com.myidis.response.ForecastCompareRes;

@Service
public class ForecastServlet {
	@Autowired
	private AnalysisServlet anaServlet;
	
	public ArrayList<ArrayList<Double>> forecast(BBReq req) {
		
		//若指标数组为空或没有数据，返回null
		if(req.getAnalysisQuota() == null || req.getAnalysisQuota().length <= 0)
			return null;
		
		//建立时间序列类TimeSequence集合，并进行数据完整性检查，不需要进行季节调整
		ArrayList<TimeSequence> sequenceList = anaServlet.checkListIntegrality(req, req.isSeasonAdjust());
		
		int count = req.getFrequency() == 1? 12: 4;
		
		ArrayList<ArrayList<Double>> res = new ArrayList<ArrayList<Double>>();
		for(TimeSequence sequence: sequenceList) {
			res.add(new NeuralNetwork(sequence.getValueList(), count, 1).predict(count));
		}
		
		return res;
	}
	
	public ForecastCompareRes compare(SeasonAdjustReq req) {
		
		ForecastCompareRes res = new ForecastCompareRes();
		
		//若指标id为0，说明数据错误，返回null
		if(req.getQuota() == 0)
			return null;
		
		//进行数据完整性检查
		TimeSequence sequence = anaServlet.checkIntegrality(new QuotaDataFinder(req.getQuota(), req));
		//若不通过返回null
		if(sequence == null)
			return null;
		
		int count = req.getFrequency() == 1? 12: 4;
		
		

		//进行季节调整
		SeasonAdjuster adjust = new SeasonAdjuster(sequence, req.getFrequency(), 1);
		
		ArrayList<Double> xlist = adjust.getTCList();
		
		NeuralNetwork network = new NeuralNetwork(xlist, count, 1);
		
		res.setxList(xlist);
		
		ArrayList<Double> forecast = network.compare(12);
		
		res.setForecastList(forecast);

		
		return res;
	}
}
